﻿using HANS.DataBaseTool;
using HANS_BackStage.Model.DbModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS.Utility.Extensions;

namespace HANS_BackStage.Impl
{
   public class RoleServices
    {
        private static HANS_BackStage.DAL.BackStageDataEntityDAL<HANS_BackStage.Model.DbModel.Backstage_role> roleDAL = new DAL.BackStageDataEntityDAL<Model.DbModel.Backstage_role>();

        /// <summary>
        /// 返回所有角色
        /// </summary>
        /// <returns></returns>
        public static List<Backstage_role> All() {
            return roleDAL.List();
        }

        /// <summary>
        /// 添加角色及其权限
        /// </summary>
        /// <param name="permissionList"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static Msg Insert(List<Backstage_permission> permissionList, Backstage_role role)
        {
            Msg msg = new Msg();
            if (roleDAL.List(new Backstage_role() { Rolename=role.Rolename}).Count==0)
            {
                if (roleDAL.Insert(role) > 0)
                {
                    msg = Role_PermiServices.InsertRolePermmission(role.Id, permissionList);
                    if (msg.Result)
                    {
                        msg.AddMsg("添加成功！");
                    }
                    else
                    {
                        msg.AddMsg("角色添加成功，但权限添加失败！");
                    }
                }
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("角色已存在！");
            }


            return msg;
        }

        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="permissionList"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static Msg Update(List<Backstage_permission> permissionList, Backstage_role role) {
            Msg msg = new Msg();
            if (roleDAL.Update(role)>0)
            {
                msg = Role_PermiServices.InsertRolePermmission(role.Id, permissionList);
                if (msg.Result)
                {
                    msg.AddMsg("修改成功！");
                }
                else
                {
                    msg.AddMsg("角色修改成功，但权限添加失败！");
                }
            }
            else
            {
                msg.Result = false;
                msg.AddMsg("角色修改失败！");
            }


            return msg;
        }
        /// <summary>
        /// 根据角色id获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Backstage_role GetEntityById(string id) {
            return roleDAL.Load(id);
        }

        /// <summary>
        /// 根据角色ID删除角色并删除权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Msg DelteRoleById(string id)
        {
            Msg msg = new Msg();
            msg = Role_PermiServices.DelteRolePermissionByRoleId(id);
            if (msg.Result)
            {
                if (roleDAL.Delete(id)>0)
                {
                    msg.AddMsg("删除成功!");
                }
                else
                {
                    msg.Result = false;
                    msg.AddMsg("删除权限成功，但删除角色失败！");
                }
                roleDAL.Delete(id);
            }
            else
            {
                msg.AddMsg("删除失败，删除角色权限失败！");
            }

            return msg;
        }
    }
}
